//
//  RootTableViewController.m
//  ThreadLock
//
//  Created by Yasin on 16/2/23.
//  Copyright © 2016年 Yasin. All rights reserved.
//

#import "RootTableViewController.h"
#import "BaseControl.h"
@interface RootTableViewController ()
{
    NSArray *dateArray;
}
@end

@implementation RootTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    dateArray = @[@"atomic",@"NSLock",@"synchronized",@"DispatchSemaphore",@"NSCondition",@"NSConditionLock",@"NSRecursiveLock",@"POSIX",@"OSSpinLock",@"dispatch_barrier_async",@"dispatch_barrier_sync",@"整体测试"];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.title = @"ThreadLock";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return dateArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rootCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"rootCell"];
    }
    cell.textLabel.text = dateArray[indexPath.row];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    id class = NSClassFromString([NSString stringWithFormat:@"%@Control",dateArray[indexPath.row]]);
    if ([class class] && [[class class] isSubclassOfClass:[BaseControl class]]) {
        
        BaseControl *control = [[class alloc] init];
        control.title = dateArray[indexPath.row];
        [control getImageNameWithMultiThread];
        
        self.tableView.userInteractionEnabled = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.tableView.userInteractionEnabled = YES;
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        });
    }
    if (indexPath.row == dateArray.count-1) {
        NSArray *dateArray = @[@"NSLock",@"synchronized",@"DispatchSemaphore",@"NSCondition",@"NSConditionLock",@"NSRecursiveLock",@"POSIX",@"OSSpinLock",@"dispatch_barrier_async",@"dispatch_barrier_sync"];
        [dateArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            //没过5秒进行一项测试，防止CPU占用冲突
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3*idx * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                id class = NSClassFromString([NSString stringWithFormat:@"%@Control",obj]);
                if ([class class]) {
                    BaseControl *control = [[class alloc] init];
                    control.title = obj;
                    [control getImageNameWithMultiThread];
                }
                if (idx == dateArray.count-1) {
                    printf("\n\n\n");
                }
            });
            
        }];
        
        self.tableView.userInteractionEnabled = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.tableView.userInteractionEnabled = YES;
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        });
    }
}

@end
